//
//  CYQuestionAndAnswerViewController.m
//  cyydt
//
//  Created by chu on 2019/1/16.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYQuestionAndAnswerViewController.h"
#import "CYJingPinWenDaChildViewController.h"
#import "CYSearchViewController.h"
#import "CYPubilishQuestionViewController.h"

@interface CYQuestionAndAnswerViewController ()<SGTopScrollMenuDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *cateArr;

@property (nonatomic, strong) SGTopScrollMenu *topMenu;

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) UIButton *xuanfuBtn;

@end

@implementation CYQuestionAndAnswerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"精品问答";
    [self getQuestionCate];
    
    // 创建底部滚动视图
    self.mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0, 44, kWindowW, kWindowH - Height_NavBar - 44);
    _mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.cateArr.count, 0);
    _mainScrollView.backgroundColor = [UIColor clearColor];
    // 开启分页
    _mainScrollView.pagingEnabled = YES;
    // 没有弹簧效果
    _mainScrollView.bounces = NO;
    // 隐藏水平滚动条
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    // 设置代理
    _mainScrollView.delegate = self;
    [self.view addSubview:_mainScrollView];
    
    [self RightsetupNavgationItemWithpictureName:@"zxm_ss"];
    
    [self.view addSubview:self.xuanfuBtn];
    [self.view bringSubviewToFront:self.xuanfuBtn];
}

- (void)RighttouchEvent{
    //搜索
    CYSearchViewController *search = [[CYSearchViewController alloc] init];
    [[AppDelegate sharedAppDelegate] pushViewController:search];
}

// 显示控制器的view
- (void)showVc:(NSInteger)index {
    
    CGFloat offsetX = index * self.view.frame.size.width;
    
    UIViewController *vc = self.childViewControllers[index];
    // 1 计算滚动的位置
    self.mainScrollView.contentOffset = CGPointMake(offsetX, 0);
    
    // 判断控制器的view有没有加载过,如果已经加载过,就不需要加载
    if (vc.isViewLoaded) return;
    
    [self.mainScrollView addSubview:vc.view];
    vc.view.frame = CGRectMake(offsetX, 0, self.view.frame.size.width, self.mainScrollView.frame.size.height);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    // 计算滚动到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.bounds.size.width;
    // 1.添加子控制器view
    [self showVc:index];
    [self.topMenu selectLabel:self.topMenu.allTitleLabel[index]];
    [self.topMenu setupTitleCenter:self.topMenu.allTitleLabel[index]];
}

#pragma mark - SGTopScrollMenuDelegate
- (void)SGTopScrollMenu:(SGTopScrollMenu *)topScrollMenu didSelectTitleAtIndex:(NSInteger)index{
    [self showVc:index];
}

- (void)setChildViewControllers{
    if (self.childViewControllers.count == 0) {
        for (int i = 0; i < self.cateArr.count; i++) {
            CYJingPinWenDaChildViewController *child = [[CYJingPinWenDaChildViewController alloc] init];
            NSDictionary *dic = self.cateArr[i];
            child.cateId = dic[@"id"];
            if (i == 0) {
                child.view.frame = CGRectMake(0, 0, kWindowW, self.mainScrollView.frame.size.height);
                [self.mainScrollView addSubview:child.view];
            }
            [self addChildViewController:child];
        }
    }
}


- (void)getQuestionCate{
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"cate-gory/ask"];
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"获取问答分类 ---- %@",responseResult);
        //        if ([self.tableView.mj_header isRefreshing]) {
        //            [self.tableView.mj_header endRefreshing];
        //        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if ([responseResult[@"data"] isKindOfClass:[NSArray class]]) {
                    NSArray *data = responseResult[@"data"];
                    NSDictionary *tuijian = @{@"id":@"0", @"name":@"推荐"};
                    [self.cateArr addObject:tuijian];
                    [self.cateArr addObjectsFromArray:data];
                }
                NSMutableArray *titles = [NSMutableArray arrayWithCapacity:0];
                for (NSDictionary *dic in self.cateArr) {
                    [titles addObject:dic[@"name"]];
                }
                [self setChildViewControllers];
                [self.view addSubview:self.topMenu];
                self.topMenu.titlesArr = titles;
                self->_mainScrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.cateArr.count, 0);

            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (NSMutableArray *)cateArr{
    if (!_cateArr) {
        _cateArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _cateArr;
}

- (SGTopScrollMenu *)topMenu{
    if (!_topMenu) {
        _topMenu = [SGTopScrollMenu topScrollMenuWithFrame:CGRectMake(0, 0, kWindowW , 44) NormalColor:RGBOF(0x999999) SelColor:RGBOF(0xF43636)];
        _topMenu.topScrollMenuDelegate = self;
    }
    return _topMenu;
}

- (UIButton *)xuanfuBtn{
    if (!_xuanfuBtn) {
        _xuanfuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_xuanfuBtn setAdjustsImageWhenHighlighted:NO];
        _xuanfuBtn.frame = CGRectMake(kWindowW - 70, kWindowH - 200, 50, 50);
        [_xuanfuBtn setImage:[UIImage imageNamed:@"jpwd_tw"] forState:UIControlStateNormal];
        [_xuanfuBtn addTarget:self action:@selector(zixunAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _xuanfuBtn;
}

- (void)zixunAction:(UIButton *)sender{
    if (![YLUserInfo isLogIn]) {
        LoginViewController *login = [[LoginViewController alloc] init];
        [[AppDelegate sharedAppDelegate] pushViewController:login];
        return;
    }
    CYPubilishQuestionViewController *chat = [[CYPubilishQuestionViewController alloc] init];
    [[AppDelegate sharedAppDelegate] pushViewController:chat];
}


@end
